/*
 * Copyright 2012 Netflix, Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.netflix.exhibitor.core.config.none;

import com.netflix.exhibitor.core.activity.ActivityLog;
import com.netflix.exhibitor.core.config.ConfigCollection;
import com.netflix.exhibitor.core.config.ConfigProvider;
import com.netflix.exhibitor.core.config.LoadedInstanceConfig;
import com.netflix.exhibitor.core.config.PropertyBasedInstanceConfig;
import com.netflix.exhibitor.core.config.PseudoLock;
import org.apache.curator.utils.CloseableUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

// TODO

public class NoneConfigProvider implements ConfigProvider
{
    private final File directory;
    private final Properties defaultProperties;

    private static final String     FILE_NAME = "exhibitor.properties";

    public NoneConfigProvider(String directory)
    {
        this(directory, new Properties());
    }

    public NoneConfigProvider(String directory, Properties defaultProperties)
    {
        this.defaultProperties = defaultProperties;
        this.directory = new File(directory);
    }

    @Override
    public void start() throws Exception
    {
        // NOP
    }

    @Override
    public void close() throws IOException
    {
        // NOP
    }

    @Override
    public LoadedInstanceConfig loadConfig() throws Exception
    {
        File            propertiesFile = new File(directory, FILE_NAME);
        Properties      properties = new Properties();
        if ( propertiesFile.exists() )
        {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(propertiesFile));
            try
            {
                properties.load(in);
            }
            finally
            {
                CloseableUtils.closeQuietly(in);
            }
        }
        PropertyBasedInstanceConfig config = new PropertyBasedInstanceConfig(properties, defaultProperties);
        return new LoadedInstanceConfig(config, propertiesFile.lastModified());
    }

    @Override
    public LoadedInstanceConfig storeConfig(ConfigCollection config, long compareVersion) throws Exception
    {
        File                            propertiesFile = new File(directory, FILE_NAME);
        PropertyBasedInstanceConfig     propertyBasedInstanceConfig = new PropertyBasedInstanceConfig(config);

        long                            lastModified = 0;
        OutputStream                    out = new BufferedOutputStream(new FileOutputStream(propertiesFile));
        try
        {
            propertyBasedInstanceConfig.getProperties().store(out, "Auto-generated by Exhibitor");
            lastModified = propertiesFile.lastModified();
        }
        finally
        {
            CloseableUtils.closeQuietly(out);
        }

        return new LoadedInstanceConfig(propertyBasedInstanceConfig, lastModified);
    }

    @Override
    public PseudoLock newPseudoLock() throws Exception
    {
        return new PseudoLock()
        {
            @Override
            public boolean lock(ActivityLog log, long maxWait, TimeUnit unit) throws Exception
            {
                return true;
            }

            @Override
            public void unlock() throws Exception
            {
                // NOP
            }
        };
    }
}
